package com.iot08.push.utils;

import com.alibaba.fastjson.JSONObject;
import com.iot08.common.utils.StringUtil;
import com.iot08.push.mob.MobConfig;
import lombok.extern.slf4j.Slf4j;
import okhttp3.Credentials;
import org.apache.commons.codec.binary.Base64;

import java.nio.charset.Charset;

/**
 * @version 1.0
 * @Description:
 * @time: 2020/11/30 10:44
 * @author: Rock
 */
@Slf4j
public class StringTool {

    /**
     * 判断推送的消息的版本是否是正常的 (什么包名,什么版本支持Mob推送)
     * @param mobileType
     * @param version
     * @time: 2020/11/30 10:55
     * @author: Rock
     * @version 1.0
     */
    public static boolean isNewVersionToPush(String packageName, String mobileType, String version) {
        if (StringUtil.isNotBlank(mobileType) && StringUtil.isNotBlank(version)) {
            if (StringUtil.isNotBlank(packageName) && MobConfig.mobPushConfigMap != null) {
                try{
                    if(MobConfig.mobPushConfigMap.keySet().contains(packageName)){
                        JSONObject item = MobConfig.mobPushConfigMap.getJSONObject(packageName);

                        if (mobileType.contains("ios")) {
                            String ios_v = item.getString(MobConfig.LASTVERSION_IOS);
                            if (compareVersions(version, ios_v) >= 0){
                                return true;
                            }else{
                                log.info("iOS 老版本,不支持MOB推送(最低支持版本ios_v={}) version={} packageName={}", ios_v, version, packageName);
                            }
                        }else if (mobileType.contains("android")){
                            String android_v = item.getString(MobConfig.LASTVERSION_ANDROID);
                            if (compareVersions(version, android_v) >= 0){
                                return true;
                            }else{
                                log.info("android 老版本,不支持MOB推送(最低支持版本android_v={}) version={} packageName={}", android_v, version, packageName);
                            }
                        }

                    }else {
                        //没配置,那么默认都不支持
                        log.info("mobPushConfigMap中找不到对应包 ,不支持 mobPushConfigMap={} ,packageName={} ", MobConfig.mobPushConfigMap, packageName);
                        return false;
                    }
                }catch (Exception e){
                    log.error("判断什么包名,什么版本支持Mob推送错误:"+e.getMessage(), e);
                }
            }

        }
        return false;
    }

    /**
     * 比较字符串 "X.X.X" 版本号
     * -1:  v1 < v2
     * 0:   相等
     * 1:   v1 > v2
     * @param version1
     * @param version2
     * @return
     */
    public static int compareVersions(String version1, String version2) {
        String[] parts1 = version1.split("\\.");
        String[] parts2 = version2.split("\\.");

        int length = Math.max(parts1.length, parts2.length);

        for (int i = 0; i < length; i++) {
            int num1 = i < parts1.length ? Integer.parseInt(parts1[i]) : 0;
            int num2 = i < parts2.length ? Integer.parseInt(parts2[i]) : 0;

            if (num1 < num2) {
                return -1;
            } else if (num1 > num2) {
                return 1;
            }
        }
        return 0;
    }
    
    public static void main(String[] args){
        String username = "bcd4e2a356ed93d5";
        String password = "avyZSRNSW7gD7rCmw6Vu4oHt0d5Bq6jCtpJvZqZAmcG";

        System.out.println(Credentials.basic(username, password));
    }

}
